package com.gylang.im.core.service;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * dto 和 do 互转
 *
 * @author gylang
 * data 2020/11/25
 * @version v0.0.1
 */
public interface IConverter<D, T> {

    D doConverterDTO(T t);

    T doConverterDO(D d);

    default List<D> converterDTOList(Collection<T> list) {
        if (null != list) {
            return list.stream().map(this::converterDTO).collect(Collectors.toList());
        } else {
            return null;
        }
    }

    default D converterDTO(T t) {
        if (null != t) {
            return doConverterDTO(t);
        } else {
            return null;
        }
    }

    default T converterDO(D d) {
        if (null != d) {
            return doConverterDO(d);
        } else {
            return null;
        }
    }

    default List<T> converterDOList(Collection<D> list) {
        if (null != list) {
            return list.stream().map(this::converterDO).collect(Collectors.toList());
        } else {
            return null;
        }
    }
}
